//
//  FullMaskView.m
//  BaseLib
//
//  Created by CPX on 2018/5/21.
//  Copyright © 2018年 CPX. All rights reserved.
//

@interface ContentView :UIView
@end
@implementation ContentView
@end

#import "FullMaskView.h"

/**
 基类全屏遮罩View
 @author yzl
 */
@interface FullMaskView()<UIGestureRecognizerDelegate>

@end

@implementation FullMaskView


-(instancetype)initWithFrame:(CGRect)frame{
    CGSize size= [UIScreen mainScreen].bounds.size;
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    self = [super initWithFrame:rect];
    if (self) {
        self.removeOnTouchOutside = YES;
        [self initUI];
        return self;
    }
    return nil;
}

- (void)initUI{
    _bgView = [UIView new];
    _bgView.frame = self.bounds;
    [self addSubview:_bgView];
    
    _contentView = [ContentView new];
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onGesture)];
    gesture.delegate = self;
    _contentView.frame = self.bounds;
    [_contentView addGestureRecognizer:gesture];
    [self addSubview:_contentView];
    
    _bgView.backgroundColor = [UIColor blackColor];
}

- (void)show:(BOOL)animate{
    __weak typeof(self) weakSelf = self;
    if (animate) {
        _bgView.alpha = 0;
        [UIView animateWithDuration:.4 animations:^{
            weakSelf.bgView.alpha = .5f;
        }];
    }
    else{
        _bgView.alpha = .5f;
    }
    self.tag = 10001;
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

- (void)onGesture{
    if (self.removeOnTouchOutside) {
        [self removeFromSuperview];
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

{
    
    // 输出点击的view的类名
    
//    NSLog(@"%@", NSStringFromClass([touch.view class]));
    
    // 若为UITableViewCellContentView（即点击了tableViewCell），则不截获Touch事件
    
//    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]||
//        [NSStringFromClass([touch.view class]) isEqualToString:@"UITableView"]) {
//
//        return NO;
//
//    }
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"ContentView"]) {
        return YES;
    }
    
    return  NO;
    
}

@end
